var app = getApp();
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var search = require('../../components/search/search.js');
var MPage = require('../MPage.js');

Page(common.extendPage(MPage, {
  data: {
    dataLoaded: true,
    isCatHide: true,

    curTy: 0,
    curTyName: '',
    catList: [],
    goodsList: [],

    page: 1,
    loading: false,
    moreLoading: false,
    noMore: false,

    from: '',
    q: '',
    serchTip: '点击搜索开始',
  },


  onShareAppMessage: function() {
    return {
      title: this.data.shop.shareTitle,
      path: "pages/index/index"
    }
  },

  onLoad: function(options) {
    var _self = this;

    _self.setData({
      curTy: options.ty,
      from: options.from,
    });

    if (options.from == 'search') {
      search.init({
        page: _self,
        onSearch(q) {
          _self.q = q;
          _self._getList({
            from: options.from,
            q,
            page: 1,
          });
        }
      });
    } else {
      _self._getList();
    }
  },

  _switchCat(evt) {
    var catId = evt.target.dataset.catId;
    var formId = evt.detail.formId;
    var catList = this.data.catList || [];
    var subCates;

    catList.some((cate) => {
      if (cate.category_id == catId) {
        subCates = cate.sub_category_list || [];
        return true;
      }
    });

    this.setData({
      curTy: catId,
      subCates,
      curSubTy: 0,
      page: 1,
      isCatHide: true,
    });

    this._getList();
    common.submitForm(formId);
  },

  _switchSubCat(evt) {
    var catId = evt.target.dataset.catId;

    this.setData({
      curSubTy: catId,
      page: 1,
    });

    this._getList();
  },


  _getList(options) {
    var _self = this;
    var {
      isMore,
      from,
      q
    } = options || {};
    var page = (options && options.page) || _self.data.page || 1;
    var {
      curSubTy,
      curTy
    } = this.data;
    var ajaxParams,
      ajaxUrl = '/openapi/CHome/categoryPage';

    if (isMore) {
      _self.setData({
        moreLoading: true
      });
    } else {
      _self.setData({
        loading: true
      });
    }

    if (from == 'search') { //搜索
      ajaxUrl = '/openapi/CSearch/getList';
      ajaxParams = {
        key: q || '',
        page
      };
    } else {
      ajaxUrl = '/openapi/CHome/categoryPage';
      ajaxParams = {
        catId: curSubTy && curSubTy != 0 ? curSubTy : curTy,
        page
      };
    }

    ajax.G(ajaxUrl, ajaxParams, function(data) {
      var {
        catList = [], goodsList = [], parentCatId, catId, parentCatIdTitle,
      } = data.result;
      var oldCatList = _self.data.catList;
      let subCates;

      var toData = {
        goodsList: isMore ? _self.data.goodsList.concat(goodsList) : goodsList,
        // catList,
        noMore: goodsList.length == 0,
        page: _self.data.page + 1,
        curTyName: parentCatIdTitle,
      };

      if (!oldCatList || oldCatList.length == 0) {
        toData.catList = catList;
        toData.curTy = parentCatId;
        toData.curSubTy = catId == parentCatId ? 0 : catId;

        catList.some((cate) => {
          if (cate.category_id == parentCatId) {
            subCates = cate.sub_category_list || [];
            return true;
          }
        });
        if (subCates) {
          toData.subCates = subCates;
        }
      }

      if (toData.goodsList.length == 0 && from == 'search') {
        toData['serchTip'] = '没有找到相关商品~';
      } else {
        toData['serchTip'] = '';
      }

      toData[isMore ? 'moreLoading' : 'loading'] = false;
      _self.setData(toData);
    });
  },

  /**
   * 加载更多
   */
  _loadMore: function() {
    if (this.data.moreLoading || this.data.loading || this.data.noMore) {
      return;
    }
    var {
      from,
      q
    } = this.data;

    this._getList({
      isMore: true,
      from,
      q: this.q
    });
  },

  _showCat: function(evt) {
    var formId = evt.detail.formId;
    this.setData({
      isCatHide: !this.data.isCatHide
    });
    common.submitForm(formId);
  },

  _bodyTap(evt) {
    if (evt.target.dataset.tagName == 'curCat') {
      return;
    }
    this.setData({
      isCatHide: true
    });
  },
  _jump: function(evt) {
    common.jump(evt);
  }
}))